package com.hiwan.unions.activity.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.system.ImageViewActivity;
import com.hiwan.unions.adapter.AddressAdapter;
import com.hiwan.unions.adapter.bean.AddressBean;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llg on 2016/4/5.
 */
public class AddressActivity extends RootActivity {
    private ListView myListview;
    private TextView ItemCityView;
    private AddressAdapter adapter;
    private ArrayList<AddressBean> arry_address = new ArrayList<AddressBean>();
    AddressBean bean;

    public static void startPreview(Context context) {
        Intent intent = new Intent(context, AddressActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_city;
    }

    @Override
    protected void initData() {
        if (getIntent().getExtras() != null) {
            bundle = getIntent().getExtras();
        }
        stringArrayList(Utils.getJson(this, "address.txt"));
        adapter.notifyDataSetChanged();
    }

    private void stringArrayList(String s) {
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "RECORDS");
            for (int i = 0; i < jsonArray.length(); i++) {
                jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                bean = new AddressBean();
                bean.setTitle(JsonUtil.getString(jsonObject, "area_name"));
                bean.setAddressUrl(JsonUtil.getString(jsonObject, "url"));
                arry_address.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void ButtonOnClick(View view) {

    }


    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        findView(R.id.viewTitle).setVisibility(View.GONE);
        findView(R.id.citButton).setVisibility(View.GONE);
        ItemCityView = findView(R.id.ItemCityView);
        ItemCityView.setText(userInfoC.getString(ConstantsValues.CITYNAMEOne, "成都市"));
        myListview = findView(R.id.myListview);
        adapter = new AddressAdapter(this, arry_address, userInfoC);
        myListview.setAdapter(adapter);
        myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IntentConfirm(position, arry_address);
            }
        });
        setTitleTxt("市州动态");

    }


    protected void IntentConfirm(int position, List<AddressBean> list) {
        String url = list.get(position).getAddressUrl();
        if (CheckUtils.isEmpty(url)) {
            ImageViewActivity.startPreview(this);
        } else {
            Utils.callHelpDeilsActivity(url, this, "");
        }
    }


    // --离线数据
    private void local() {

    }

}
